﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IGGCore;
using IGGWindowApp.Data;

namespace IGGWindowApp.Panels
{
    public partial class PanelStandings : UserControl
    {
        private const int Y_EASTDIVISION = 42;
        private const int Y_CENTRALDIVISION = 152;
        private const int Y_WESTDIVISION = 302;


        public PanelStandings()
        {
            InitializeComponent();
        }

        public void SetData(Scoreboard board)
        {
            bool spring = (board.StandingsGrapefruit != null && board.StandingsGrapefruit.Count > 0);
            if (board.StandingsNLE != null && board.StandingsNLE.Count > 0)
            {
                CreateConference("National League", true);
                CreateConference("American League", false);

                CreateDivision("East", true, board.StandingsNLE, Y_EASTDIVISION, spring);
                CreateDivision("Central", true, board.StandingsNLC, Y_CENTRALDIVISION, spring);
                CreateDivision("West", true, board.StandingsNLW, Y_WESTDIVISION, spring);

                CreateDivision("East", false, board.StandingsALE, Y_EASTDIVISION, spring);
                CreateDivision("Central", false, board.StandingsALC, Y_CENTRALDIVISION, spring);
                CreateDivision("West", false, board.StandingsALW, Y_WESTDIVISION, spring);
            }
            else
            {
                int column = 9;
                int startY = 50;
                int incr = 17;

                CreateConference("Grapefruit League", true);
                foreach (StandingTeam stand in board.StandingsGrapefruit)
                {
                    PanelTeamStanding team = new PanelTeamStanding(stand, spring);
                    team.Location = new System.Drawing.Point(column, startY);
                    this.Controls.Add(team);
                    startY += incr;
                }


                CreateConference("Cactus League", false);
                column = 364;
                startY = 50;
                foreach (StandingTeam stand in board.StandingsCactus)
                {
                    PanelTeamStanding team = new PanelTeamStanding(stand, spring);
                    team.Location = new System.Drawing.Point(column, startY);
                    this.Controls.Add(team);
                    startY += incr;
                }
            }
        }

        private void CreateConference(string title, bool columnLeft)
        {
            int column = 8;
            if (columnLeft == false)
            {
                column = 363;
            }

            Label labelConf = new Label();
            labelConf.AutoSize = true;
            labelConf.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Bold);
            labelConf.Location = new System.Drawing.Point(column, 13);
            labelConf.Size = new System.Drawing.Size(135, 21);
            labelConf.Text = title;
            this.Controls.Add(labelConf);
        }

        private void CreateDivision(string title, bool columnLeft, List<StandingTeam> teams, int divisionPos, bool spring)
        {
            int column = 9;
            if (columnLeft == false)
            {
                column = 364;
            }
            Label labelDiv = new Label();
            labelDiv.AutoSize = true;
            labelDiv.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            labelDiv.Location = new System.Drawing.Point(column, divisionPos);
            labelDiv.Size = new System.Drawing.Size(35, 17);
            labelDiv.Text = title;
            this.Controls.Add(labelDiv);

            PanelStatsHeader header = new PanelStatsHeader(spring);
            header.Location = new System.Drawing.Point(column + 123, divisionPos);
            this.Controls.Add(header);
            int startY = divisionPos + 20;
            int incr = 17;
            foreach (StandingTeam stand in teams)
            {
                PanelTeamStanding team = new PanelTeamStanding(stand, spring);
                team.Location = new System.Drawing.Point(column, startY);
                this.Controls.Add(team);
                startY += incr;
            }
        }



        private class PanelTeamStanding : Panel
        {
            private Label m_team;
            private Label m_wins;
            private Label m_losses;
            private Label m_gb;
            private Label m_l10;
            private Label m_strk;

            public PanelTeamStanding(StandingTeam team, bool spring)
            {
                this.Size = new System.Drawing.Size(308, 19);
                m_team = CreateLabel(team.team, 2);
                m_wins = CreateLabel(team.w, 123);
                m_losses = CreateLabel(team.l, 155);
                if (spring)
                {
                    m_gb = CreateLabel(team.pct, 188);
                    m_l10 = CreateLabel("", 219);
                    m_strk = CreateLabel("", 258);
                }
                else
                {
                    m_gb = CreateLabel(team.gb, 188);
                    m_l10 = CreateLabel(team.last10, 219);
                    m_strk = CreateLabel(team.strk, 258);
                }


                this.Controls.Add(m_team);
                this.Controls.Add(m_wins);
                this.Controls.Add(m_losses);
                this.Controls.Add(m_gb);
                this.Controls.Add(m_l10);
                this.Controls.Add(m_strk);
            }

            private Label CreateLabel(string text, int xpos)
            {
                Label lbl = new Label();
                lbl.AutoSize = true;
                lbl.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lbl.Location = new System.Drawing.Point(xpos, 0);
                lbl.Size = new System.Drawing.Size(37, 17);
                lbl.Text = text;
                return lbl;
            }
        }

        private class PanelStatsHeader : Panel
        {
            private Label m_wins;
            private Label m_losses;
            private Label m_gb;
            private Label m_l10;
            private Label m_strk;

            public PanelStatsHeader(bool spring)
            {
                this.Size = new System.Drawing.Size(165, 17);
                m_wins = CreateLabel("W", 1);
                m_losses = CreateLabel("L", 36);
                m_gb = CreateLabel("GB", 64);
                m_l10 = CreateLabel("L10", 94);
                m_strk = CreateLabel("STRK", 128);
                
                this.Controls.Add(m_wins);
                this.Controls.Add(m_losses);
                this.Controls.Add(m_gb);
                if (spring)
                {
                    m_gb.Text = "PCT";
                }
                else
                {
                    this.Controls.Add(m_l10);
                    this.Controls.Add(m_strk);
                }
            }

            private Label CreateLabel(string text, int xpos)
            {
                Label lbl = new Label();
                lbl.AutoSize = true;
                lbl.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lbl.Location = new System.Drawing.Point(xpos, 0);
                lbl.Size = new System.Drawing.Size(37, 17);
                lbl.Text = text;
                return lbl;
            }
        }
    }


}
